package com.dfrz.problem.action.role;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.dispatcher.SessionMap;

import com.dfrz.problem.model.Role;
import com.dfrz.problem.model.User;
import com.dfrz.problem.service.RoleService;
import com.dfrz.problem.utils.IConstant;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class RoleAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private User user;
	private RoleService roleService;
	private Role role;
	private List<Role> roleList;
	private int roleId;
	
	public String addRole(){
		return SUCCESS;
	}
	
	//获取角色列表
	public String roleList(){
    	roleList = roleService.roleList();
    	if(roleList != null){
    		Iterator<Role> it = roleList.iterator();    		
    		while(it.hasNext()){
    			Role role = it.next();
    			if(role.getRoleId()==1 && role.getRoleName().equals("未分配")){
    				it.remove();
    			}
    		}
    	}
		return SUCCESS;
	}
	
	//角色信息传参
	public String roleInfo(){	
		role = roleService.getRole(roleId);
		return SUCCESS;
	}
	//角色信息修改
	public String updataSucces(){	
		boolean b = roleService.updateRoleInfo(role);
		if(b == false)
		{
			return INPUT;
		}
		return SUCCESS;
	}
	
	//修改角色
	public String checkUpdate(){
		Boolean b = roleService.updateRoleInfo(role);
		if (b != true) {
			addFieldError("eamilErro", getText(IConstant.ROLE_EXIST));
			return INPUT;
		}
		return SUCCESS;
	}
	
	public String checkAddRole(){
		role.setCreateTime(new Date());
		Boolean b = roleService.addRole(role);
		if (b != true) {
			addFieldError("eamilErro", getText(IConstant.ROLE_EXIST));
			return INPUT;
		}
		return SUCCESS;
	}
	
	public void setRole(Role role) {
		this.role = role;
	}
	
	public Role getRole() {
		return role;
	}
	
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	public RoleService getRoleService() {
		return roleService;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	public List<Role> getRoleList() {
		return roleList;
	}
	
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	
	public User getUser() {
		SessionMap<String, Object> session = (SessionMap<String, Object>) ActionContext
				.getContext().getSession();
		user = (User) session.get("user");
		return user;
	}
}
